package com.xuanrui.commons;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author sunke
 * @Date 2020/11/17 22:19
 * @description
 */

public class HttpHelper {

    static Pattern p = Pattern.compile("\\s*|\t|\r|\n");


    public static <T> T getBodyToObject(HttpServletRequest request, Class<T> clazz) {

        try {
            String jsonStr = getBodyString(request);

            Matcher m = p.matcher(jsonStr);
            jsonStr = m.replaceAll("");
            return JSONObject.parseObject(jsonStr, clazz);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;


    }


    public static String getBodyString(HttpServletRequest request) throws IOException {
        StringBuilder sb = new StringBuilder();
        InputStream inputStream = null;
        BufferedReader reader = null;
        try {
            inputStream = request.getInputStream();
            reader = new BufferedReader(
                    new InputStreamReader(inputStream, Charset.forName("UTF-8")));

            char[] bodyCharBuffer = new char[1024];
            int len = 0;
            while ((len = reader.read(bodyCharBuffer)) != -1) {
                sb.append(new String(bodyCharBuffer, 0, len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
}
